package fr.insta.reverse.uml.controller;

import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.insta.reverse.uml.bean.User;
import fr.insta.reverse.uml.routing.Route;
import fr.insta.reverse.uml.service.ServiceFactory;
import fr.insta.reverse.uml.service.ServiceName;
import fr.insta.reverse.uml.service.ServiceUser;
import fr.insta.reverse.uml.util.IhmValues;
import fr.insta.reverse.uml.util.InstaUtils;

public class UserManagerController extends DefaultController
{
	ServiceUser serviceUser = (ServiceUser) ServiceFactory.getService(ServiceName.ServiceUser);

	public UserManagerController(HttpServletRequest _request, HttpServletResponse _response)
	{
		super(_request, _response);
	}

	@Override
	public void execute()
	{
		String route = (String) request.getAttribute("route");

		if (route.contentEquals(Route.UserInscription.getRoute()))
		{
			pageInscription();
		}
		else if (route.contentEquals(Route.UserConnection.getRoute()))
		{
			pageConnection();
		}
		else if (route.contentEquals(Route.UserUpdate.getRoute()))
		{
			pageUpdate();
		}
		else if (route.contentEquals(Route.UserDeconnection.getRoute()))
		{
			pageDeconnection();
		}

	}

	private void pageInscription()
	{
		User user = new User();

		request.setAttribute("user", user);

		if (request.getMethod() == "POST")
		{
			String errorMessage;

			user.setNom(request.getParameter(IhmValues.FORM_INSCRIPTION_NOM));
			user.setPrenom(request.getParameter(IhmValues.FORM_INSCRIPTION_PRENOM));
			user.setMail(request.getParameter(IhmValues.FORM_INSCRIPTION_EMAIL));

			user.setPassword(request.getParameter(IhmValues.FORM_INSCRIPTION_PASSWORD));
			user.setPasswordConfirm(request.getParameter(IhmValues.FORM_INSCRIPTION_PASSWORD_CONFIRM));

			if ((errorMessage = serviceUser.getErrorMessageForNewUser(user)) != null)
			{
				setMessage(errorMessage);
				dispatch("userInscription");
				return;
			}

			try
			{
				user.setPassword(InstaUtils.sha1(user.getPassword()));
			}
			catch (NoSuchAlgorithmException e)
			{
				e.printStackTrace();
			}

			if (serviceUser.insert(user) != null)
			{
				setMessage("L'inscription a bien été effectuée.");
				dispatch("accueil");
			}
			else
			{
				setMessage("Une erreur a eu lieue lors de l'inscription.");
				dispatch("userInscription");
			}
		}

		dispatch("userInscription");
	}

	private void pageConnection()
	{
		if (request.getMethod() == "POST")
		{
			try
			{
				request.setAttribute("loginSucceed", Boolean.FALSE);

				String message;

				User user = new User();

				user.setMail(request.getParameter(IhmValues.FORM_LOGIN_EMAIL));
				user.setPassword(InstaUtils.sha1(request.getParameter(IhmValues.FORM_LOGIN_PASSWORD)));

				if ((message = serviceUser.getErrorMessageForConnection(user)) == null)
				{
					user = serviceUser.connect(user);

					if (user != null)
					{

						serviceUser.setSessionUser(request, user);

						message = "Bienvenue " + user.getNom() + " " + user.getPrenom();
						request.setAttribute("loginSucceed", Boolean.TRUE);
					}
					else
					{
						message = "Identifiant ou mot de passe incorrect";
					}
				}

				request.setAttribute("loginMessage", message);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				request.setAttribute("loginMessage", "Une erreur inatendue a eu lieue");
			}

			/*request.setAttribute("loginSucceed", Boolean.TRUE);
			request.setAttribute("loginMessage", "TEST MESSAGE");*/

			request.setAttribute("redirectionTime", 3);
			request.setAttribute("redirectionRoute", Route.Accueil);

			dispatch("userLogin");
		}
	}

	private void pageUpdate()
	{
		//POST ***************
		if (request.getMethod() == "POST")
		{

			User user = new User();
			user.setNom(request.getParameter(IhmValues.FORM_UPDATEUSER_NOM));
			user.setPrenom(request.getParameter(IhmValues.FORM_UPDATEUSER_PRENOM));
			user.setPassword(request.getParameter(IhmValues.FORM_UPDATEUSER_PASSWORD));
			user.setMail(request.getParameter(IhmValues.FORM_UPDATEUSER_EMAIL));

			serviceUser.update(user);

			setMessage("Données mises à jour");
			dispatch("userDonnees");
		}
	}

	private void pageDeconnection()
	{
		serviceUser.logoutUser(request);

		request.setAttribute("redirectionTime", 3);
		request.setAttribute("redirectionRoute", Route.Accueil);

		dispatch("userLogout");
	}
}
